
const { SettleProcessMgr } = require("./settleProcessMgr");
const { SIGNAL_DEFINE } = require("./signal");

class DXSettleProcessMgr extends SettleProcessMgr {

    constructor() {
        super();
        this.totalLoseScore = 0;
    }

    recordData() {
        var gamberIndexs = this.seatMgr.getGamberIndexs();
        var gamberNum = this.seatMgr.getGamberNum();
        var backcount = 0;
        var roomInfo = this.gameMgr.getRoomInfo();

        var results = [];
        if(roomInfo.allRecord == null){
            roomInfo.allRecord = [];
            roomInfo.allRecord[0] = {};
        }
        var record;
        if (!this.gameMgr.isInfiniteRound()) {
            var currentRound = this.gameMgr.getCurrentRound();
            record = roomInfo.allRecord[currentRound] = {};
        }
        else {
            record = roomInfo.allRecord = {};
        }
        var cost_coins = function (data,user) {
            db.rate_coins(data,function (info) {
                var baseScore = gameMgr.getBaseScore();
                console.log(user.userId, "目前金币为", info.coins, "目前底分为", baseScore);
                
                if(info.coins <= gameMgr.getBaseScore()){
                    this.seatMgr.sitDown(user.userId, false);
                }

                data.coins = info.coins;
                data.score = -1 * info.cost;
                record[data.userId].score = -1 * info.cost;
                backcount ++;
                console.log("结算数据", record);

                //如果局数已够，则进行整体结算，并关闭房间
                if(backcount >= gamberNum){
                    this.signal.emit(SIGNAL_DEFINE.GAME_OVER, results, record, this.gameMgr.isRoomOver());
                }
            });
        }
        for(let i = 0; i < gamberIndexs.length; ++i){
            let index = gamberIndexs[i];
            let user = this.seatMgr.getUserByIndex(index);
            let gamber = this.seatMgr.getGamberByIndex(index);
            if(user.shangzhuang == null){
                user.shangzhuang = 0;
            }
            if(user.win == null){
                user.win = 0;
            }
            user.shangzhuang += this.banker == index ? 1 : 0;
            user.win += gamber.score > 0 ? 1 : 0;

            user.coins += gamber.score;
            user.score += gamber.score;
            user.ready = false;

            record[gamber.userId] = {score:gamber.score,holds:gamber.holds,niu:0,isZhuang:this.banker == index};

            var userRT = {
                userId:user.userId,
                area : user.area,
                coins:user.coins,
                score:gamber.score,
            };
            results.push(userRT);
            dbusers.push(user.userId);
            dbresult.push(gamber.score);

            // console.log("计算金币", userRT, rs);
            cost_coins(userRT,user);
        }
    }

    doAction() {
        this.recordData();
        if (this.gameMgr.isRoomOver()) {
            if(!this.gameMgr.isInfiniteRound()) {
                this.signal.emit(SIGNAL_DEFINE.SAVE_GAME_HISTORY);
            }
        } else {
            this.signal.emit(SIGNAL_DEFINE.NEXT_GAME);
            //记录打牌信息
            this.signal.emit(SIGNAL_DEFINE.SAVE_GAME_RECORD);
        }
        this.doNextAction();
    }
}

exports.DXSettleProcessMgr = DXSettleProcessMgr;